<?php
/*
*	Kleophatra - Open Source Content Management System
*	------------------------------------------------------------------------
*	Copyright (C) 2007-2009, Sergio Milardovich <smilardovich@yahoo.com.ar>
*	This program is Free Software.
*
*	@package	Kleophatra
*	@subpackage	/lib
*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
*	@author		Sergio Milardovich <smilardovich@yahoo.com.ar>
*	@link		http://www.kleophatra.org
*/
	interface iplugins{
		public function index($buff,&$buff);
	}


	class kleoplugins extends kleotemplate{
		var $buff;
		var $plugin_forms;

		function load_plugins($buff,&$buff){
			$this->buff = $buff;
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."plugins WHERE status = 1");
			while($row = $this->fetch_array($query)){
				$name = $row['file'];
				if(is_file(KLEO_PLUGINS_DIR.$name.'/lang/'.KLEO_LANG.'/main.php')){
					require_once KLEO_PLUGINS_DIR.$name.'/lang/'.KLEO_LANG.'/main.php';
				} else if(is_file(KLEO_PLUGINS_DIR.$name.'/lang/'.KLEO_LANG.'/main.php')){
					require_once KLEO_PLUGINS_DIR.$name.'/lang/'.KLEO_LANG.'/main.php';
				}
				$file = KLEO_PLUGINS_DIR.$name.'/main.php';
				$this->apploadlib($file, $name, $plugin);
				/* Check the class */
				$check = class_implements($plugin);
				/* Check the interface */
				if($check['iplugins'] == null){
					$this->apperror("Plugin($name) Error: iplugins interface not implemented", "common");
				}
				/* Basic plugin functions */
				$plugin->index($buff,$buff);
				/* on_head() */
				if(method_exists($plugin,'on_head')){
					$buff = str_replace("</head>", $plugin->on_head()."\n</head>", $buff);
				}
				/* on_body() */
				if(method_exists($plugin,'on_body')){
					$buff = str_replace("<body>", "<body>\n".$plugin->on_body(), $buff);
				}
				/* on_ending() */
				if(method_exists($plugin,'on_ending')){
					$buff = str_replace("</html>", $plugin->on_ending()."\n</html>", $buff);
				}
				$BPOST = array_flip($_POST);
				foreach($BPOST as $post){
					if(method_exists($plugin, $post) && !$this->is_native_function($post)){
						$plugin->$post();
					}
				}
				if(isset($plugin->plugin_replaces) && is_array($plugin->plugin_replaces)){
					$this->plugin_replace($plugin->plugin_replaces,$buff,$buff);
				}
			}

		}

		function add_form($plugin,$title,$file,$callback){
			$this->plugin_forms[$plugin][] = array("title" => $title,"file" => $file,"callback" => $callback);
		}

		function is_native_function($string){
			$functions = array("plugin_remove","plugin_install","on_ending","on_body","on_head","index");
			foreach($functions as $function){
				if($function == $string){
					$f = true;
				}
			}
			if(isset($f)){
				return true;
			} else {
				return false;
			}
		}

		function put_after($tag,$content,$buff){
			$buff = str_replace($tag, $tag."\n".$content, $buff);
			return $buff;
		}

		function put_before($tag,$content,$buff){
			$buff = str_replace($tag, $content."\n".$tag, $buff);
			return $buff;
		}

		function put_over($tag,$content,$buff){
			$buff = str_replace($tag, $content, $buff);
			return $buff;
		}

		function plugin_replace($codes,$buff,&$buff){
 			foreach($codes as $basic => $value){
				$buff = str_replace($basic,$value,$buff);
			}
		}
		
		function load_plugin_info($file){
			$url = KLEO_PLUGINS_DIR.$file.'/package.xml';
			if(file_exists($url)){
				$feed = file_get_contents($url);
				$this->apploadlib(KLEO_LIB_DIR."third_party/xml2array.lib.php", "xmltoarray", $xmltoarray);
				$package = $xmltoarray->xml2array($feed);
				return $package['package'];
			} else {
				return false;
			}
		}
		function get_plugin_name($id){
			$query = $this->query("SELECT file FROM ".KLEO_TABLE_PREFIX."plugins WHERE pluid = $id");
			$row = $this->fetch_array($query);
 			return $row['file'];
		}
		function activate_plugin($id){
			$this->apploadlib(KLEO_PLUGINS_DIR.$this->get_plugin_name($id).'/main.php', $this->get_plugin_name($id), $plugin);
			if(method_exists($plugin, 'plugin_install')){
				$plugin->plugin_install();
			}
			$this->query("UPDATE `".KLEO_TABLE_PREFIX."plugins` SET `status` = '1' WHERE `pluid` =$id LIMIT 1 ;");
		}
		function deactivate_plugin($id){
			$this->apploadlib(KLEO_PLUGINS_DIR.$this->get_plugin_name($id).'/main.php', $this->get_plugin_name($id), $plugin);
			if(method_exists($plugin, 'plugin_remove')){
				$plugin->plugin_remove();
			}
			$this->query("UPDATE `".KLEO_TABLE_PREFIX."plugins` SET `status` = '0' WHERE `pluid` =$id LIMIT 1 ;");
		}
		function add_plugin($file,$status=0){
			$this->query("INSERT INTO `".KLEO_TABLE_PREFIX."plugins` (
					`pluid` ,
					`file` ,
					`status`
				) VALUES (
					NULL , '$file', '$status'
				);");
		}
		function check_plugins(){
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."plugins");
			while($row = $this->fetch_array($query)){
				if(!file_exists(KLEO_PLUGINS_DIR.$row['file'].'/package.xml')){
					$this->delete_plugin($row['file']);
				}
			}
		}
		function is_added_plugin($name){
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."plugins WHERE file = '$name'");
			if($this->num_rows($query) > 0){
				return true;
			} else {
				return false;
			}
		}
		function is_active_plugin($name){
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."plugins WHERE file = '$name' AND status = 1");
			if($this->num_rows($query) > 0){
				return true;
			} else {
				return false;
			}
		}
		function is_plugin($name){
			if(file_exists(KLEO_PLUGINS_DIR.$name.'/package.xml')){
				return true;
			} else {
				return false;
			}
		}
		/*
		 * This function is for calling functions in an specific section of the pannel
		 * $type variable can be 'onpost', 'once' or 'nopost'
		 */
		function add_queue($callback,$file,$place='*',$type='nopost'){
			$this->actions[] = array(
						"type" => $type,
						"callback" => $callback,
						"place" => $place,
						"file" => $file
					);
		}


	}



?>
